//
//  AppDelegate.m
//  MyProject
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "GoodsMainViewController.h"
#import "ElephantMainViewController.h"
#import "MyMainViewController.h"
#import "QualityGoodsMainViewController.h"
#import "ActicleMainViewController.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
#import "UserLeadViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    
    [ShareSDK registerApp:@"ae83db668f48"
          activePlatforms:@[//@(SSDKPlatformTypeSinaWeibo),
                            // @(SSDKPlatformTypeTencentWeibo),
                            //  @(SSDKPlatformTypeFacebook),
                            // @(SSDKPlatformTypeTwitter),
                            @(SSDKPlatformTypeWechat),
                            //@(SSDKPlatformTypeQQ),
                            // @(SSDKPlatformTypeDouBan),
                            //@(SSDKPlatformTypeKaixin),
                            // @(SSDKPlatformTypePocket),
                            @(SSDKPlatformTypeRenren),
                            //@(SSDKPlatformTypeMail),
                            //@(SSDKPlatformTypeSMS),
                            // @(SSDKPlatformTypeCopy)
                            ]
                 onImport:^(SSDKPlatformType platformType) {
                     switch (platformType) {
                             //                         case SSDKPlatformTypeTencentWeibo:
                             //                             [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                             //                             break;
                         case SSDKPlatformTypeWechat:
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         case SSDKPlatformTypeQQ:
                             [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                             break;
                      //   case SSDKPlatformTypeRenren:
                           //  [ShareSDKConnector connectRenren:[RennClient class]];
                             break;
                         default:
                             break;
                     }
                 }
     
     
     //红色字体号码2:想分享到什么平台就去什么开放平台申请开发者账号 得到对应的一个或多个码 对应的填入
     //腾讯家族貌似需要跳转分享(sso)(模拟器并不能模拟因为木有微信等软件)
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              
              switch (platformType) {
                  case SSDKPlatformTypeTencentWeibo:
                      [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650"
                                                   appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                                 redirectUri:@"http://www.sharesdk.cn"];
                      break;
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@"wx3ed8351a09968c4d"
                                            appSecret:@"f403a15e6de28b3e9ac89495f8dfe57a"];
                      break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@"1104911770"
                                           appKey:@"Z0lHz6vF0NY2VrrM"
                                         authType:SSDKAuthTypeBoth];
                      break;
                      //                  case SSDKPlatformTypeFacebook:
                      //                      [appInfo SSDKSetupFacebookByAppKey:@"107704292745179"
                      //                                               appSecret:@"38053202e1a5fe26c80c753071f0b573"
                      //                                                authType:SSDKAuthTypeBoth];
                      //                      break;
                      //                  case SSDKPlatformTypeTwitter:
                      //                      [appInfo SSDKSetupTwitterByConsumerKey:@"LRBM0H75rWrU9gNHvlEAA2aOy"
                      //                                              consumerSecret:@"gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"
                      //                                                 redirectUri:@"http://www.sharesdk.cn"];
                      //                      break;
                      //                  case SSDKPlatformTypeDouBan:
                      //                      [appInfo SSDKSetupDouBanByApiKey:@"02e2cbe5ca06de5908a863b15e149b0b"
                      //                                                secret:@"9f1e7b4f71304f2f"
                      //                                           redirectUri:@"http://www.sharesdk.cn"];
                      //                      break;
                  case SSDKPlatformTypeRenren:
                      [appInfo SSDKSetupRenRenByAppId:@"481436"
                                               appKey:@"b559c4e31ee04df99a325d6b323415a1"
                                            secretKey:@"a7ab1fd5aea54c7b9ea100d1e4163918"
                                             authType:SSDKAuthTypeBoth];
                      break;
                      //                  case SSDKPlatformTypeGooglePlus:
                      //                      [appInfo SSDKSetupGooglePlusByClientID:@"232554794995.apps.googleusercontent.com"
                      //                                                clientSecret:@"PEdFgtrMw97aCvf0joQj7EMk"
                      //                                                 redirectUri:@"http://localhost"
                      //                                                    authType:SSDKAuthTypeBoth];
                      //                      break;
                      //                  case SSDKPlatformTypePocket:
                      //                      [appInfo SSDKSetupPocketByConsumerKey:@"11496-de7c8c5eb25b2c9fcdc2b627"
                      //                                                redirectUri:@"pocketapp1234"
                      //                                                   authType:SSDKAuthTypeBoth];
                      //                      break;
                  default:
                      break;
              }
          }];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"m"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"m"];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isWhite"];
        UserLeadViewController *userlead =[[UserLeadViewController alloc]init];
        self.window.rootViewController = userlead;
           }
    
    else{
        //美物推荐:
        GoodsMainViewController *recoCV = [[GoodsMainViewController alloc]init];
        UINavigationController *recoNavi = [[UINavigationController alloc]initWithRootViewController:recoCV];
        recoCV.tabBarItem.image = [UIImage imageNamed:@""];
        recoCV.title = @"美物推荐";
        
        
        
        //精选推物:
        QualityGoodsMainViewController *qualityCV = [[QualityGoodsMainViewController alloc]init];
        UINavigationController *qualityNavi = [[UINavigationController alloc]initWithRootViewController:qualityCV];;
        qualityCV.tabBarItem.image = [UIImage imageNamed:@""];
        qualityCV.title = @"精选推物";
        
        
        //个人主页:
        MyMainViewController *myCV = [[MyMainViewController alloc]init];
        UINavigationController *myNavi = [[UINavigationController alloc]initWithRootViewController:myCV];
        myCV.tabBarItem.image = [UIImage imageNamed:@""];
        myCV.title = @"个人主页";
        
        
        //大象推文:
        ElephantMainViewController *elephantCV = [[ElephantMainViewController alloc]init];
        UINavigationController *elephantNavi = [[UINavigationController alloc]initWithRootViewController:elephantCV];
        elephantCV.tabBarItem.image = [UIImage imageNamed:@""];
        elephantCV.title = @"大象推文";
        
        
        //美文推荐:
        ActicleMainViewController *acticleCV = [[ActicleMainViewController alloc]init];
        UINavigationController *acticleNavi = [[UINavigationController alloc]initWithRootViewController:acticleCV];
        acticleCV.tabBarItem.image = [UIImage imageNamed:@""];
        acticleCV.title = @"美文推荐";
        
        
        UITabBarController *tabC = [[UITabBarController alloc]init];
        tabC.viewControllers = @[recoNavi,qualityNavi,myNavi,elephantNavi,acticleNavi];

    
    self.window.rootViewController = tabC;
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "DLLanou.MyProject" in the application's documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyProject" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    // Create the coordinator and store
    
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyProject.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return _persistentStoreCoordinator;
}


- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

@end
